<?php
/**
 * 次品管理器
 * User: poptser@foxmail.com
 * Date: 2018/1/30
 * Time: 14:05
 */
namespace App\Http\Controllers\WarehouseManage;
use App\Http\Controllers\ApiController;
use App\Services\SortingWeb\ToolService;
use App\Services\Warehouse\DirectTransfersService;
use App\Services\Warehouse\WarehouseLocalService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

//次品管理控制器
class CipinController extends ApiController{

    public $userId;
    public $wid;
    public $toolService;
    public $widInfo;
    public $kindeeSn;
    protected $WarehouseLocalService;   //库存相关
    public $DirectTransfersService;

    public function __construct(ToolService $toolService,WarehouseLocalService $warehouseLocalService,
                                DirectTransfersService $directTransfersService){


        $isLogion = Auth::guard('sorting')->check();
        if(!$isLogion){
            $this->returnErr("api_token 不存在或已过期");
        }else{
            $this->userId = Auth::guard('sorting')->id();
            if($this->checkUserType(3,$this->userId)){
                $this->returnErr("您没有访问此接口得权限");
            }else{
                $this->toolService = $toolService;
                $this->wid = $this->toolService->getUserWid($this->userId);
                $this->widInfo = $this->toolService->getWidInfo($this->wid);
                $this->kindeeSn = $this->widInfo->k3_number;
                $this->WarehouseLocalService=$warehouseLocalService;    //库存相关
                $this->DirectTransfersService=$directTransfersService;  //次品
            }
        }
    }

    /**
     * 获取商品详情
     * Author:poptser@foxmail.com
     * @param Request $request
     * @return mixed
     */
    public function getGoodsInfo(Request $request){
        $goodSn = $request->goodSn;
        if(empty($goodSn)){
            return $this->failedReturn("goodSn 为空");
        }
        $goodsInfo = DB::table("goods")->where("goods_sn",$goodSn)
            ->select(['goods_img','goods_name','goods_sn','supplier_code as k3_supplier_sn','consignment as k3_vmi'])
            ->first();
        if(empty($goodsInfo)){
            return $this->failedReturn("未找到此商品");
        }
        $goodsToKingdee = [];
        $goodsToKingdee[$goodsInfo->goods_sn] = [
            "goods_sn"=>$goodsInfo->goods_sn,
            "k3_vmi"=>$goodsInfo->k3_vmi,
            "k3_supplier_sn"=>$goodsInfo->k3_supplier_sn,
        ];

        $jindeeData =$this->WarehouseLocalService->CheckGoodsUsableWarehouse($goodsToKingdee,$this->wid);
        //return $jindeeData;
        if($jindeeData){
            $goodsInventory = (int)$jindeeData[$goodsInfo->goods_sn];
        }else{
            $goodsInventory = "无库存记录";
        }
        $goodsInfo = [
            "goodName"=>$goodsInfo->goods_name,
            "goodSn"=>$goodsInfo->goods_sn,
            "goodImg"=>$goodsInfo->goods_img,
            "goodInventory"=>$goodsInventory,
            "consignment"=>$goodsInfo->k3_vmi
        ];
        return $this->success($goodsInfo);
    }

    /**
     * 次品录入
     * Author:poptser@foxmail.com
     * @param Request $request
     * @return mixed
     */
    public function setGoodsPoor(Request $request){

        $goodsListHelp = [];    //代销商品组
        $goodsListBuy = [];    //购销商品组

        $goodsList = json_decode($request->goodsList,true);
        if($goodsList){

            foreach ($goodsList as $goods){
                $goodsInfo = DB::table("goods")->where("goods_sn",$goods['goodSn'])
                    ->select(['spec_unit','supplier_code','consignment'])
                    ->first();
                if($goodsInfo){
                    $goodsOne = [
                        "goods_sn"=>$goods['goodSn'],
                        "goods_number"=>$goods['goods_number'],
                        "spec_unit"=>$goodsInfo->spec_unit,
                        "k3_supplier_sn"=>$goodsInfo->supplier_code,
                        "k3_vmi"=>$goodsInfo->consignment,
                    ];
                    if($goodsInfo->consignment==1){
                        $goodsListHelp[] = $goodsOne;
                    }else{
                        $goodsListBuy[] = $goodsOne;
                    }
                }
            }
        }else{
            return $this->failedReturn("JSON 错误");
        }

        $successGoods = [];

        for ($i=0;$i<2;$i++){
            if($i==0){
                $postGoods = $goodsListHelp;
            }else{
                $postGoods = $goodsListBuy;
            }
            if(!empty($postGoods)){
                $data=[
                    'wid'=>$this->wid,
                    'goods'=>$postGoods,
                    'note'=>$request->remark
                ];
                try{
                    $return=$this->DirectTransfersService->DefectiveGoodsEntry($data);
                    if($return){
                        foreach ($postGoods as $one){
                            $successGoods[] = $one['goods_sn'];
                        }
                    }
                }catch (\Exception $exception){
                    return $this->failedReturn($exception->getMessage());
                }
            }
        }

        return $this->success($successGoods);
    }
}